package btp.Calendars;


public class RataDie {
	public static final int SUNDAY = 0;
	public static final int MONDAY = 1;
	public static final int TUESDAY = 2;
	public static final int WEDNESDAY = 3;
	public static final int THURSDAY = 4;
	public static final int FRIDAY = 5;
	public static final int SATURDAY = 6;

	public static int dayOfWeek(long rd)
	{
		int result = (int) (rd%7);
		return result<0?result+7:result ;
	}

	public static long dayOnOrBefore(int day, long rd){
		return rd-dayOfWeek(rd-day);
	}

	public static long dayOnOrAfter(int day, long rd){
		return dayOnOrBefore(day, rd+6);
	}

	public static long dayNearest(int day, long rd){
		return dayOnOrBefore(day, rd+3);
	}

	public static long dayBefore(int day, long rd){
		return dayOnOrBefore(day, rd-1);
	}

	public static long dayAfter(int day, long rd){
		return dayOnOrBefore(day, rd+7);
	}

	public static long nthDay(int n, int day, long rd){
		return 7*n+ ((n>0)?dayBefore(day, rd):dayAfter(day, rd));
	}

	public static String getStringForDay(int day){
		switch(day){
		case SUNDAY: return "Sunday";
		case MONDAY: return "Monday";
		case TUESDAY: return "Tuesday";
		case WEDNESDAY: return "Wednesday";
		case THURSDAY: return "Thursday";
		case FRIDAY: return "Friday";
		case SATURDAY: return "Saturday";
		}
		return null;
	}

}
